﻿Imports System.Data.OleDb
''' <summary>
''' Contains code for searching for books.s
''' </summary>
''' <author>Feng Jiang, Rick Simpson, AJ Whatling</author>
Partial Class Search

    Inherits System.Web.UI.Page

    ''' <summary>
    ''' Called when the "Search" button is clicked.  Search is performed here.
    ''' </summary>
    Protected Sub SearchSubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchSubmitButton.Click
        'Server.Transfer("~/ErrorPage.aspx")
        Dim UserSearchQuery As String = SearchTerms.Text
        Dim UserSearchParam As String = SearchParameter.SelectedValue.ToString

        'Dim MyConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\mis431library.mdb;Persist Security Info=True")
        'Dim MyCommand As OleDbCommand
        Dim MyCommand As String
        LibraryBookDataTier.Initialize()

        'MyConnection.Open()

        If (UserSearchParam.ToLower = "title") Then
            MyCommand = "SELECT book_ID, ISBN, Title, author_Name, book_ID FROM Books WHERE Title LIKE '%" & UserSearchQuery.ToString & "%'"
        ElseIf (UserSearchParam.ToString = "isbn") Then
            MyCommand = "SELECT book_ID, ISBN, Title, author_Name, book_ID FROM Books WHERE ISBN LIKE '%" & UserSearchQuery.ToString & "%'"
        Else
            MyCommand = "SELECT book_ID, ISBN, Title, author_Name, book_ID FROM Books WHERE author_Name LIKE '%" & UserSearchQuery.ToString & "%'"
        End If

        ' Add table headers
        Dim HeaderContainRow = New WebControls.TableHeaderRow()

        Dim BookIDHeaderCell = New WebControls.TableHeaderCell
        BookIDHeaderCell.Text = "Book ID"
        BookIDHeaderCell.Attributes.Add("class", "odd")
        Dim ISBNHeaderCell = New WebControls.TableHeaderCell
        ISBNHeaderCell.Text = "ISBN"
        ISBNHeaderCell.Attributes.Add("class", "even")
        Dim TitleHeaderCell = New WebControls.TableHeaderCell
        TitleHeaderCell.Text = "Book Title"
        TitleHeaderCell.Attributes.Add("class", "odd")
        Dim AuthorHeaderCell = New WebControls.TableHeaderCell
        AuthorHeaderCell.Text = "Author"
        AuthorHeaderCell.Attributes.Add("class", "even")

        HeaderContainRow.Cells.Add(BookIDHeaderCell)
        HeaderContainRow.Cells.Add(ISBNHeaderCell)
        HeaderContainRow.Cells.Add(TitleHeaderCell)
        HeaderContainRow.Cells.Add(AuthorHeaderCell)

        SearchResultsTable.Rows.Add(HeaderContainRow)

        ' Opn the data reader oject.
        Dim reader As OleDbDataReader = LibraryBookDataTier.ExecuteSelect(MyCommand)
        Dim Zebra As Boolean = True
        While reader.Read()
            Dim Zebra_String As String

            If (Zebra) Then
                Zebra_String = "even"
            Else
                Zebra_String = "odd"
            End If

            Dim SDR = reader
            Dim ContainingTableRow As WebControls.TableRow
            ContainingTableRow = New WebControls.TableRow

            Dim BookIDSearchCell As WebControls.TableCell
            Dim TitleSearchCell As WebControls.TableCell
            Dim ISBNSearchCell As WebControls.TableCell
            Dim AuthorSearchCell As WebControls.TableCell

            BookIDSearchCell = New WebControls.TableCell()
            TitleSearchCell = New WebControls.TableCell()
            ISBNSearchCell = New WebControls.TableCell()
            AuthorSearchCell = New WebControls.TableCell()

            ' Item(0) is Book_ID
            ' Item(1) is ISBN
            ' Item(2) is Title
            ' Item(3) is Author
            BookIDSearchCell.Text = SDR.Item(0).ToString

            ISBNSearchCell.Text = SDR.Item(1).ToString
            ISBNSearchCell.Attributes.Add("class", Zebra_String)

            ' Create a hyperlink to info about the book
            Dim TitleHyperlink As WebControls.HyperLink = New WebControls.HyperLink
            TitleHyperlink.Attributes.Add("href", "BookInfo.aspx?bid=" & SDR.Item(4).ToString)
            TitleHyperlink.Text = SDR.Item(2).ToString

            TitleSearchCell.Controls.Add(TitleHyperlink)
            TitleSearchCell.Attributes.Add("class", Zebra_String)

            AuthorSearchCell.Text = SDR.Item(3).ToString
            AuthorSearchCell.Attributes.Add("class", Zebra_String)

            ContainingTableRow.Cells.Add(BookIDSearchCell)
            ContainingTableRow.Cells.Add(ISBNSearchCell)
            ContainingTableRow.Cells.Add(TitleSearchCell)
            ContainingTableRow.Cells.Add(AuthorSearchCell)

            SearchResultsTable.Rows.Add(ContainingTableRow)
            Zebra = Not Zebra
        End While

        'MyConnection.Close()
        LibraryBookDataTier.CloseDown()
    End Sub
End Class
